package com.test2015.Swing.component;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class HighLightTextField extends JTextField implements FocusListener{
	
	private int diameter = 16;
	private Color blueC1 = new Color(Integer.parseInt("66afe9",16));
	private Color blueC2 = new Color(Integer.parseInt("d6eaf9",16));
	private Color blueC3 = new Color(Integer.parseInt("e1f0fb",16));
	private Color blueC4 = new Color(Integer.parseInt("eaf4fc",16));

	private Color borderC = new Color(Integer.parseInt("cccccc",16));
	private boolean haveFocus = false;
	
	public HighLightTextField(){
		
		this.addFocusListener(this);
	}
	
	@Override
	public Insets getMargin() {
		return new Insets(4, diameter/2, 4, diameter/2);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
//		this.setBackground(this.getParent().getBackground());
		this.setBackground(Color.red);
		this.setOpaque(false);
		super.paintComponent(g);
	}
	
	@Override
	protected void paintBorder(Graphics g) {
//		super.paintBorder(g);
		Graphics2D g2d = (Graphics2D)g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		int compw = this.getWidth();
		int comph = this.getHeight();
		
//		g.setColor(Color.black);
//		g.drawRect(0, 0, compw, comph);
		if(!haveFocus){
			g.setColor(borderC);
			g.drawRoundRect(4, 4, compw-8,comph-8, diameter, diameter);
		}else{
			g.setColor(blueC1);
			int x=4;
			int y=4;
			int w=compw-8;
			int h=comph-8;
			g.drawRoundRect(x,y,w,h, diameter, diameter);
			
			g.setColor(blueC2);
			x= x -1;
			y= y - 1;
			w=compw-x*2;
			h=comph-y*2;
			g.drawRoundRect(x,y,w,h, diameter, diameter);
			
			g.setColor(blueC3);
			x= x -1;
			y= y - 1;
			w=compw-x*2;
			h=comph-y*2;
			g.drawRoundRect(x,y,w,h, diameter, diameter);
			
			g.setColor(blueC4);
			x= x -1;
			y= y - 1;
			w=compw-x*2;
			h=comph-y*2;
			g.drawRoundRect(x,y,w,h, diameter, diameter);
		}
	}
	
	@Override
	public void focusGained(FocusEvent focusevent) {
		haveFocus = true;
		this.repaint();
	}

	@Override
	public void focusLost(FocusEvent focusevent) {
		haveFocus = false;
		this.repaint();
	}
	
	public static void main(String[] args) {
		
		JFrame frame = new JFrame();
		JPanel jPanel = new JPanel();
		
		HighLightTextField txtf = new HighLightTextField();
		txtf.setPreferredSize(new Dimension(300,70));
		jPanel.add(txtf);
		jPanel.add(new JButton("AAAAAAAAAAAAA"));
		
//		jPanel.setLayout(null);
		Container contentPane = frame.getContentPane();
		contentPane.add(jPanel);
		
		
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		frame.setSize(400, 300);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

}
